﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;

namespace cal24.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.mvvmlight.net
    /// </para>
    /// </summary>
    public class MainViewModel : NotifyPropertyChanged
    {
        private int _num1;
        public int num1 { get { return _num1; } set { SetProperty(ref  _num1, value); } }

        private int _num2;
        public int num2 { get { return _num2; } set { SetProperty(ref  _num2, value); } }

        private int _num3;
        public int num3 { get { return _num3; } set { SetProperty(ref  _num3, value); } }

        private int _num4;
        public int num4 { get { return _num4; } set { SetProperty(ref  _num4, value); } }

        private string _displayOut = string.Empty;
        public string displayOut { get { return _displayOut; } set { SetProperty(ref  _displayOut, value); } }

        private string _imagepath_1 = "pic/back.png";
        public string imagepath_1 { get { return _imagepath_1; } set { SetProperty(ref _imagepath_1, value); } }
        private string _imagepath_2 = "pic/back.png";
        public string imagepath_2 { get { return _imagepath_2; } set { SetProperty(ref _imagepath_2, value); } }
        private string _imagepath_3 = "pic/back.png";
        public string imagepath_3 { get { return _imagepath_3; } set { SetProperty(ref _imagepath_3, value); } }
        private string _imagepath_4 = "pic/back.png";
        public string imagepath_4 { get { return _imagepath_4; } set { SetProperty(ref _imagepath_4, value); } }


        public string calString;
        public int leftNumber;
        public int rightNumber;
        public bool prepareLeftNumber;
        public bool prepareCal;
        public Button calbtn;
        public int answerIndex;
        public bool isrember;

        static int[] ten_pic_index = { 10, 11, 12, 13, 23, 24, 25, 26, 36, 37, 38, 39, 49, 50, 51, 52, 53, 54 };
        static int[,] pailiearray = new int[24,4] {{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},
                                    {1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},
                                    {2,1,0,3},{2,1,3,0},{2,0,1,3},{2,0,3,1},{2,3,1,0},{2,3,0,1},
                                    {3,1,2,0},{3,1,0,2},{3,2,1,0},{3,2,0,1},{3,0,1,2},{3,0,2,1}};

        public List<string> answer = new List<string>();

        private bool cal24(int x1, int x2, int x3, int x4)
        {
            bool result = false;
            int[] data = new int[4] { x1, x2, x3, x4 };
            List<int> isCal = new List<int>();
            for(int i=0; i<24; ++i)
            {
                int[] num= new int[]{data[pailiearray[i, 0]], data[pailiearray[i, 1]], data[pailiearray[i, 2]], data[pailiearray[i, 3]]};
                int total = num[0] + num[1]*100 + num[2]*10000 + num[3]*1000000;
                if(!isCal.Contains(total))
                {
                    isCal.Add(total);
                    result |= cal(num[0], num[1], num[2], num[3]);
                }
            }
            return result;
        }

        private bool cal(int a, int b, int c, int d)
        {
            string[] cc = new string[]{"+", "-", "*", "/"};

            List<int> result1 = cal2num(cal2num(cal2num(a, b), c), d);
            List<int> result2 = cal2num(cal2num(a, b), cal2num(c, d));
            List<int> result3 = cal2num(cal2num(a, cal2num(b, c)), d);
            for(int i=0; i<result1.Count; ++i)
            {
                if(result1[i] == 24)
                {
                    answer.Add( string.Format("(({0} {1} {2}) {3} {4}) {5} {6} = 24", a, cc[i / 16], b, cc[i % 16 / 4], c, cc[i % 16 % 4], d));
                }
            }

            for (int i = 0; i < result2.Count; ++i)
            {
                if (result2[i] == 24)
                {
                    answer.Add(string.Format("({0} {1} {2}) {3} ({4} {5} {6}) = 24", a, cc[i / 16], b, cc[i % 16 % 4], c, cc[i % 16 / 4], d));
                }
            }

            for (int i = 0; i < result3.Count; ++i)
            {
                if (result3[i] == 24)
                {
                    answer.Add(string.Format("({0} {1} ({2} {3} {4})) {5} {6} = 24", a, cc[i % 16 / 4], b, cc[i / 16], c, cc[i % 16 % 4], d));
                }
            }
            return answer.Count > 0;
        }

        private List<int> cal2num(object a, object b)
        {
 	        List<int>  result = new List<int>();
            if(a is int && b is int)
            {
                int x = (int)a;
                int y = (int)b;
                if(x == -1 || y == -1)
                {
                    return new List<int>{-1,-1,-1,-1};
                }
                return new List<int>{x+y,x-y>=0?x-y:-1,x*y,y!=0 && x%y==0?x/y:-1};
            }
            if(a is int && b is List<int>)
            {
                int x= (int)a;
                List<int> y = b as List<int>;
                foreach(var yy in y)
                {
                    foreach(var z in cal2num(x, yy))
                    {
                        result.Add(z);
                    }
                }
            }
            if(a is List<int> && b is List<int>)
            {
                List<int> x= a as List<int>;
                List<int> y = b as List<int>;
                foreach(var xx in x)
                {
                    foreach(var yy in y)
                    {
                        foreach(var z in cal2num(xx,yy))
                            result.Add(z);
                    }
                }
            }
            if(a is List<int> && b is int)
            {
                List<int> x= a as List<int>;
                int y = (int)b;
                foreach(var xx in x)
                {
                    foreach(var z in cal2num(xx,y))
                          result.Add(z);
                }
            }
            return result;
        }

        public void createGame()
        {
            do{
                Random rd = new Random();
                num1 = rd.Next(1, 11);
                num2 = rd.Next(1, 11);
                num3 = rd.Next(1, 11);
                num4 = rd.Next(1, 11);

                imagepath_1 = "pic/" + switchIndex(num1) + ".jpg";
                imagepath_2 = "pic/" + switchIndex(num2) + ".jpg";
                imagepath_3 = "pic/" + switchIndex(num3) + ".jpg";
                imagepath_4 = "pic/" + switchIndex(num4) + ".jpg";

                answer.Clear();
            } while (!cal24(num1, num2, num3, num4));
        }

        public int switchIndex(int num)
        {
            Random rd = new Random();
            if (num < 10)
            {
                return num + 13 * rd.Next(0, 4);
            }
            
            return ten_pic_index[rd.Next(0, ten_pic_index.Length)];
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            createGame();
        }
    }
}